<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .time {
        width: 300px;
        height: 100px;
      }

      .time div {
        float: left;
        width: 50px;
        height: 30px;
        margin: 10px;
        background: skyblue;
        text-align: center;
        line-height: 30px;
      }
    </style>
  </head>

  <body>
    <div class="time">
      <div class="hours"></div>
      <div class="minutes"></div>
      <div class="seconds"></div>
    </div>
  </body>
</html>
<script>
  window.addEventListener("load", function () {
    var hours = document.querySelector(".hours");
    var minutes = document.querySelector(".minutes");
    var seconds = document.querySelector(".seconds");
    function countDown(time) {
      var nowTime = +new Date(); // 返回的是当前时间总的毫秒数
      var inputTime = +new Date(time); // 返回的是用户输入时间总的毫秒数
      var times = (inputTime - nowTime) / 1000; // times是剩余时间总的秒数
      // var d = parseInt(times / 60 / 60 / 24).toString().padStart(2, '0') // 天
      var h = parseInt((times / 60 / 60) % 24)
        .toString()
        .padStart(2, "0"); //时
      var m = parseInt((times / 60) % 60)
        .toString()
        .padStart(2, "0"); // 分
      var s = parseInt(times % 60)
        .toString()
        .padStart(2, "0"); // 当前的秒
      hours.innerHTML = h;
      minutes.innerHTML = m;
      seconds.innerHTML = s;
    }
    countDown("2020-9-30 18:00:00");
    setInterval(function () {
      countDown("2020-9-30 18:00:00");
    }, 1000);
  });
</script>
